#ifndef COMMON_AABB_H
#define COMMON_AABB_H

#include <string>
#include <sstream>
#include <glm/glm.hpp>
#include <limits>
namespace ShaderLab {
    class AABB {

    public:
        glm::vec3 min, max;

        AABB() {
            reset();
        }

        AABB(const glm::vec3& pt) {
            reset();
            add(pt);
        }

        void add(glm::vec3& pt) {
            min.x = std::fmin(min.x, pt.x);
            min.y = std::fmin(min.y, pt.y);
            min.z = std::fmin(min.z, pt.z);

            max.x = std::fmax(max.x, pt.x);
            max.y = std::fmax(max.y, pt.y);
            max.z = std::fmax(max.z, pt.z);
        }

        void add(const AABB& other) {
            min.x = std::fmin(min.x, other.min.x);
            min.y = std::fmin(min.y, other.min.y);
            min.z = std::fmin(min.z, other.min.z);

            max.x = std::fmax(max.x, other.max.x);
            max.y = std::fmax(max.y, other.max.y);
            max.z = std::fmax(max.z, other.max.z);
        }

        void reset() {
            min = glm::vec3(std::numeric_limits<float>::max());
            max = glm::vec3(-std::numeric_limits<float>::max());
        }

        glm::vec3 diagonal() {
            return max - min;
        }

        std::string toString() {
            std::stringstream stream;
            stream << "AABB: min= (" << min.x << ", " << min.y << ", " << min.z << ") " <<
                "max = (" << max.x << ", " << max.y << ", " << max.z << ")";
            return stream.str();
        }
    };
}

#endif // !COMMON_AABB_H
